<?php

namespace app\controller\bossManager;

use app\validate\PageValidate;
use app\validate\TableDataChangeValidate;
use app\service\TableDataChangeService;
use share\utils\Helper;

class TableDataChangeController extends WebBossController{
    public function initValidate(){        
        $this->validate = new TableDataChangeValidate();
    }

    public function data_info_action(){
        $data = TableDataChangeService::getNativeAll(['day' => [
            // ['>=',intval(date("Ymd",strtotime("-30 days")))],
            // ['<=',intval(date("Ymd"))],

            // 'exp',
            //     '`day` >= '. intval(date("Ymd",strtotime("-30 days"))).
            //     ' AND `day` <= '. intval(date("Ymd")),
            'between',[intval(date("Ymd",strtotime("-30 days"))),intval(date("Ymd"))]
        ]],[
            'db','table_name','day','num',
        ],'db asc,table_name asc,day asc');

        $days = Helper::arraySmartUnique(array_column($data,'day'));
        $res = [];
        foreach($data as $item){
            $key = $item['db'].'.'.$item['table_name'];
            if(!isset($res[$key])){
                foreach($days as $day){
                    $tmp = Helper::filterOne($data,['db' => $item['db'],'table_name' => $item['table_name'],'day'=>$day]);
                    $res[$key][] = $tmp ? $tmp['num'] : 0;
                }                
            }
        }
        $res['days'] = $days;
        return self::ok($res);
    }
}